<template>
    <div class="login-container">
      <div class="login_box">
        <div class="asicon">
          <img src="../assets/logo.png">
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginFormref" label-width="0px" class="login_form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="iconfont icon-lock_fill" type="password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary"  @click="login">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
    export default {
        name: "index",
      data(){
          return{
            loginForm:{
              username:'admin',
              password:'123456'
            },
            rules:{
              username:[{required:true,message:'请输入用户名',trigger:'blur'},
                { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
              ],
              password: [{required:true,message:'密码不能为空',trigger:'blur'}]
            }

          }
      },
      methods:{
        reset(){
          this.$refs.loginFormref.resetFields();
        },
        login(){
          this.$refs.loginFormref.validate(async valid=>{
            if(valid){
              const res=await this.$http.post('/login',this.loginForm)
             if(res.data.meta.status!==200){this.$message.error('登录失败')
             }else{
               this.$message.success('登录成功')
               window.sessionStorage.setItem('token',res.data.data.token)
               this.$router.push("./home")
             }
            }
            }

          )
        }
      }
    }
</script>

<style lang="less" scoped>
.login-container{
  background: #2c3e50;
  height: 100%;
}
  .login_box{
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 3px;
position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
  }
  .asicon{
    width: 130px;
    height: 130px;
    padding: 5px;
    border-radius: 50%;
    border: 1px solid #eeeeee;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background: #fff;
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background: #f0f0f0;
    }
  }
  .login_form{
    width: 100%;
    position: absolute;
    bottom: 0;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
</style>
